package com.wechat.it.isales.common.crop;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wechat.it.isales.common.util.ImageCutterUtil;

public class CropServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.process(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.process(req, resp);
	}

	public void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String imagePath = request.getRealPath("/") + "/attached/" + "/20131227/" + "20131227111824_238.jpg";

		String X = request.getParameter("x");
		String Y = request.getParameter("y");
		String w = request.getParameter("w");
		String h = request.getParameter("h");
		//
		String path = "E:\\localhost_workspaces\\WeSeales\\com.wechat.it.kindeditor\\src\\main\\webapp\\attached\\image\\20131227\\20131227110248_451.jpg";
		//
		int x = Integer.parseInt(X);
		int y = Integer.parseInt(Y);
		int width = Integer.parseInt(w);
		int height = Integer.parseInt(h);

		// 起始坐标，剪切大小
		// int x = 100;
		// int y = 75;
		// int width = 100;
		// int height = 100;
		// 参考图像大小
		int clientWidth = 150;
		int clientHeight = 150;

		out.println("裁剪大小  x:" + x + ",y:" + y + ",width:" + width + ",height:" + height);

		File file = new File(path);
		BufferedImage image = ImageIO.read(file);
		double destWidth = image.getWidth();
		double destHeight = image.getHeight();

		if (destWidth < width || destHeight < height) {
			out.print("源图大小小于截取图片大小!");
			return;
		}

		double widthRatio = destWidth / clientWidth;
		double heightRatio = destHeight / clientHeight;

		// x = Double.valueOf(x * widthRatio).intValue();
		// y = Double.valueOf(y * heightRatio).intValue();
		// width = Double.valueOf(width * widthRatio).intValue();
		// height = Double.valueOf(height * heightRatio).intValue();

		out.println("裁剪大小  x:" + x + ",y:" + y + ",width:" + width + ",height:" + height);

		/************************ 基于三方包解决方案 *************************/
		String formatName = ImageCutterUtil.getImageFormatName(file);
		String pathSuffix = "." + formatName;
		String pathPrefix = ImageCutterUtil.getFilePrefixPath(file);
		String targetPath = pathPrefix + System.currentTimeMillis() + pathSuffix;
		targetPath = ImageCutterUtil.cutImage(file.getPath(), targetPath, x, y, width, height);

		// String bigTargetPath = pathPrefix + System.currentTimeMillis()
		// + pathSuffix;
		// ImageCutterUtil.zoom(targetPath, bigTargetPath, 100, 100);
		//
		// String smallTargetPath = pathPrefix + System.currentTimeMillis()
		// + pathSuffix;
		// ImageCutterUtil.zoom(targetPath, smallTargetPath, 50, 50);

		/************************ 基于JDK Image I/O 解决方案(JDK探索失败) *************************/
		// File destFile = new File(targetPath);
		// ImageCutterUtil.cutImage(file, destFile, x, y, width, height);
	}
}
